<script>
	import event from 'common/event.js';
	export default {
        
		carData: [],
		longLat: '',
		isLogin: false,
        isRegisted: false,
		globalData: {
            userInfo: null,
            schoolInfo: null,
            schoolList: null,
            navHeight: 0,
            scene:null,
            bang:false,
            n: 0,
            screenHeight:0,
            schoolId: 0,
            listServiceType: '',
            windowWidth: 0,
            windowHeight: 0,
            isRegisted: false,
            countList: {},   
            qrCode:'' //支付宝扫普通码进店铺
			},
		onLaunch: function(options) {
            this.$options.globalData.scene = options.scene;
            // #ifdef MP-ALIPAY
                if(options.query && options.query.qrCode){
                    this.$options.globalData.qrCode  = options.query.qrCode;
                }
            // #endif
			this.checkMobilePhone(); 
			this.initSocket();
		},
		onShow: function() {
			console.log('App Show')
		},
		methods:{
				checkMobilePhone: function () {
                    var self = this;
                    
                    uni.getSystemInfo({
                        success: function (res) {
                            self.$options.globalData.n = res.statusBarHeight;
                            self.$options.globalData.screenHeight = res.screenHeight;
                            if ( res.screenHeight - res.windowHeight >70 ){
                                    self.$options.globalData.bang = true;  //刘海
                            }else{
                                    self.$options.globalData.bang = false;
                            }
                        }
                    })   
				},
				clear: function () {
                    uni.clearStorageSync()
				},
				openSocket: function () {
                    return false;
                    if(!socketSuccess){
                        console.log('socket 连接');
                        uni.connectSocket({url: Config.socketUrl});
                    }
				},
				
				initSocket: function () {
					var self = this;
					var heartTimer = null;
					var eventClose = false;
					//监听socket
					uni.onSocketOpen(function (res) {
                        console.log('socket 打开');
                        socketSuccess = true;
                        eventClose = false;
                        clearInterval(heartTimer);
                        heartTimer = setInterval(function () {
                                uni.sendSocketMessage({data: JSON.stringify({cmd: "heart"})})
                        }, 5000);
                        var timestamp = parseInt((new Date().getTime()) / 1000);
                        var uid = self.$options.globalData.userInfo.uid || 0;
                        var login = {
                            cmd: "login",
                            uid: uid,
                            timestamp: timestamp,
                            auth: Config.PUB_KEY
                        };
                        uni.sendSocketMessage({data: JSON.stringify(login)});
                        event.emit('socketLoginSuccess', []);
					});
					uni.onSocketMessage(function (res) {
                        var data = JSON.parse(res.data);
                        switch (data.cmd) {
                            case 'heart':
                                break;
                            case 'login':
                                break;
                            case 'notice':
                                event.emit(data.type, data.content);
                                break;
                        }
					});
					//获取导航高度
					uni.getSystemInfo({
						success: res => {
							//导航高度
                            this.$options.globalData.navHeight = res.statusBarHeight + 46;
                            this.$options.globalData.windowHeight = res.windowHeight;
                            this.$options.globalData.windowWidth = res.windowWidth;
						}, fail(err) {
							console.log(err);
						}
					});
                    
					//监听关闭
					uni.onSocketClose(function (res) {
                        if (!socketSuccess) return false;
                        clearInterval(heartTimer);
                        if (eventClose) {
                            console.log('socket 断开');
                            return false;
                        }
                        console.log('断线重连1');
                        setTimeout(function () {
                            self.openSocket();
                        }, 10000);
					});
					//断线重连
					uni.onSocketError(function (res) {
                        socketSuccess = false;
                        clearInterval(heartTimer);
                        console.log('断线重连2');
                        setTimeout(function () {
                            self.openSocket();
                        }, 10000);
					});	
					//关闭事件
					event.on('closeSocket', this, function () {
                        eventClose = true;
                        uni.closeSocket()
					});
				}
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
	/*每个页面公共css */
</style>
